package com.xy.util;

import android.os.Environment;
import android.os.StatFs;

import java.io.File;

/**
 * Created by XuYang
 * 2023/9/22
 * Email:544066591@qq.com
 */
public class SDCardUtil {

    /*
     * 判断SD卡是否存在 返回true表示存在
     */
    public static boolean availableSDCard() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    /*
     * 获取内部SD卡可用空间
     */
    public static long getSDFreeSize() {
        // 取得SD卡文件路径
        File path = Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());
        // 获取单个数据块的大小(Byte)
        long blockSize = sf.getBlockSizeLong();
        // 空闲的数据块的数量
        long freeBlocks = sf.getAvailableBlocksLong();
        // 返回SD卡空闲大小
        // return freeBlocks * blockSize; //单位Byte
        // return (freeBlocks * blockSize)/1024; //单位KB
        return (freeBlocks * blockSize) / 1024 / 1024; // 单位MB
    }
}
